$(function () {
    getUserInfo()
    var layer = layui.layer
    //点击退出按钮退出登录
    $('#btnLogout').on('click', function () {
        //提示用户是否退出
        layer.confirm('确认退出？', { icon: 3, title: '提示' }, function (index) {
            //清除登录信息
            localStorage.removeItem('token')
            location.href = './login.html'
            // window.location = 'login.html'
            layer.close(index);
        });
    })
})
// 获取用户的信息
function getUserInfo() {
    $.ajax({
        method: 'get',
        url: '/my/userinfo',
        // headers: {
        //     Authorization: localStorage.getItem('token')
        // },
        success: function (res) {
            if (res.status !== 0) {
                return layui.layer.msg(res.message)
            }
            renderAvatar(res.data)
        }
    })
}
// 渲染用户昵称和头像
function renderAvatar(data) {
    var name = data.nickname || data.username
    $('#welcome').html('欢迎&nbsp;&nbsp;' + name.toUpperCase())
    if (data.user_pic !== null) {
        $('.layui-nav-img').attr('src', data.user_pic).show()
        $('.text-avatar').hide()
    } else {
        $('.layui-nav-img').hide()
        var fist = name[0].toUpperCase()
        $('.text-avatar').html(fist).show()
    }
}

